package com.lagou.worker.service.impl;

import com.lagou.worker.api.CodeService;
import com.lagou.worker.model.LagouAuthCode;
import com.lagou.worker.service.LagouAuthCodeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName CodeServiceImpl
 * @Description TODO
 * @Author xsq
 * @Date 2020/8/17 15:11
 **/
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private LagouAuthCodeService lagouAuthCodeService;

    @Override
    public Integer validate(String email, String code) {
        //验证码验证
        LagouAuthCode lagouAuthCode = lagouAuthCodeService.validate(email);
        Long time = (System.currentTimeMillis() - lagouAuthCode.getExpiretime().getTime()) / 1000;
        if (lagouAuthCode == null) {
            return 3;
        }
        //超过10分钟验证码无效
        if (time > 10 * 60) {
            return 2;
        }
        if (code.equals(lagouAuthCode.getCode())) {
            return 0;
        } else {
            return 1;
        }
    }
}
